package edu.cs.usask.ca.silver.plugin.rcp.handlers.edit;

import org.apache.log4j.Logger;
import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.ui.handlers.HandlerUtil;

import edu.cs.usask.ca.silver.businesslogic.ids.ScenarioID;
import edu.cs.usask.ca.silver.plugin.rcp.dao.SilverBusinessLogicController;
import edu.cs.usask.ca.silver.plugin.rcp.jobs.LoggerReportScheduler;
import edu.cs.usask.ca.silver.plugin.rcp.views.projectviewer.ProjectViewI;

public class SetSelectedScenarioAsBaseLineScenario extends AbstractHandler {

	private static Logger logger = Logger.getLogger(SetSelectedScenarioAsBaseLineScenario.class);
	
	@Override
	public Object execute(ExecutionEvent event) throws ExecutionException {
		ProjectViewI view = (ProjectViewI) HandlerUtil
				.getActiveWorkbenchWindow(event).getActivePage()
				.findView(event.getParameter("projectviewid"));
		ScenarioID selectedScenarioID = (ScenarioID) view
				.getTreeSelectionAsSilverTreeObject().getSilverObjectId();

		try {

			SilverBusinessLogicController.borrowScenario()
					.setScenarioAsBaselineToParentModelVersion(
							selectedScenarioID, view.getAssociatedStorage());

		} catch (Exception e) {
			LoggerReportScheduler.scheduleErrorLogging(logger,"error while selected scenario as baseline scenario in rcp command handler", e);
		}

		return null;
	}

}
